package com.tao.plan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tao.plan.common.BaseThreadLocal;
import com.tao.plan.domain.pojo.PCountDownDay;
import com.tao.plan.domain.pojo.PMemoryDay;
import com.tao.plan.mapper.PCountDownDayMapper;
import com.tao.plan.mapper.PMemoryDayMapper;
import com.tao.plan.service.PCountDownDayService;
import com.tao.plan.service.PMemoryDayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service
public class PMemoryDayServiceImpl extends ServiceImpl<PMemoryDayMapper, PMemoryDay> implements PMemoryDayService {


	@Override
	public List<PMemoryDay> listByType(Integer type) {
		LambdaQueryWrapper<PMemoryDay> lqw = new LambdaQueryWrapper<>();
		lqw.eq(PMemoryDay::getUid, BaseThreadLocal.getUid()).eq(PMemoryDay::getType,type);
		return this.baseMapper.selectList(lqw);
	}

	@Override
	public List<PMemoryDay> listByDay(String lunar, String calendar) {
		LambdaQueryWrapper<PMemoryDay> lqw = new LambdaQueryWrapper<>();
		lqw.eq(PMemoryDay::getUid, BaseThreadLocal.getUid())
				.and(wrapper -> wrapper
						.or(lqw1 -> lqw1
								.eq(PMemoryDay::getType, 0)
								.apply("MONTH(target_time) = MONTH({0}) AND DAY(target_time) = DAY({0})", calendar)
						)
						.or(lqw2 -> lqw2
								.eq(PMemoryDay::getType, 1)
								.apply("MONTH(target_time) = MONTH({0}) AND DAY(target_time) = DAY({0})", lunar)
						)
				);

		return this.baseMapper.selectList(lqw);
	}

	@Override
	public List<PMemoryDay> listByDay(String lunar, String calendar, Long userId) {
		LambdaQueryWrapper<PMemoryDay> lqw = new LambdaQueryWrapper<>();
		lqw.eq(PMemoryDay::getUid, userId)
				.and(wrapper -> wrapper
						.or(lqw1 -> lqw1
								.eq(PMemoryDay::getType, 0)
								.apply("MONTH(target_time) = MONTH({0}) AND DAY(target_time) = DAY({0})", calendar)
						)
						.or(lqw2 -> lqw2
								.eq(PMemoryDay::getType, 1)
								.apply("MONTH(target_time) = MONTH({0}) AND DAY(target_time) = DAY({0})", lunar)
						)
				);

		return this.baseMapper.selectList(lqw);

	}
}
